<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getOrderList">
            <template #append>
              <el-button :icon="Search" @click="searchOrder" />
            </template>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表数据 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" width="200px"></el-table-column>
        <el-table-column label="订单价格(元)" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template #default="scope">
            <el-tag type="success" v-if="scope.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template #default="{ row }">
            {{ formatDate(row.create_time) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default>
            <el-button size="small" type="primary" :icon="Edit" @click="showAddressEditDialog"></el-button>
            <el-button size="small" type="success" :icon="Location" @click="showExpressDialog"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:current-page="queryInfo.pagenum"
        :page-sizes="pageSizes"
        v-model:page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 地址编辑弹层 -->
    <address-edit ref="AddressEditRef"></address-edit>
    <!-- 物流查询弹层 -->
    <express-dialog ref="ExpressDialogRef"></express-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Search, Edit, Location } from '@element-plus/icons-vue'
import { getOrderListApi } from '@/api/order'
import { message, formatDate } from '@/utils/functions'
import AddressEdit from './components/AddressEdit.vue'
import ExpressDialog from './components/ExpressDialog.vue'

const ExpressDialogRef = ref(null)
// 获取物流信息
const showExpressDialog = () => {
  ExpressDialogRef.value.open()
}

const AddressEditRef = ref(null)
// 打开修改地址对话框窗
const showAddressEditDialog = () => {
  // 显示弹层
  AddressEditRef.value.open()
}

// 分页大小
const pageSizes = ref([10, 20, 30, 40])
// 构建分页参数
const queryInfo = ref({
  query: '',
  pagenum: 1,
  pagesize: pageSizes.value[0]
})
// 订单列表数据
const orderList = ref([])
const total = ref(0)
const getOrderList = async () => {
  const data = await getOrderListApi(queryInfo.value)
  console.log(data)
  orderList.value = data.goods
  total.value = data.total
}
getOrderList()

// 监听分页参数的变化
// 每页显示的条数发生改变时触发
const handleSizeChange = size => {
  queryInfo.value.pagesize = size
  getGoodsList()
}
// 当前页码改变时触发
const handleCurrentChange = page => {
  queryInfo.value.pagenum = page
  getGoodsList()
}
</script>

<style lang="scss" scoped>
</style>